home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / briksrc.zoo / atari.c < prev    next >
C/C++ Source or Header  |  1991-05-16  |  6KB  |  157 lines

  1. #include <assert.h>
  2.  
  3. #define  FMAX  2        /* Number of different filename patterns */
  4.  
  5. #include <osbind.h>
  6. #include <stdio.h>
  7.  
  8. void fcbpath (struct _dta *, char *, char *);
  9.  
  10. /*
  11. nextfile() returns the name of the next source file matching a filespec.
  12.  
  13. INPUT
  14.    what: A flag specifying what to do.  If "what" is 0, nextfile()
  15.       initializes itself.  If "what" is 1, nextfile() returns the next
  16.       matching filename.
  17.    filespec:  The filespec, usually containing wildcard characters, that
  18.       specifies which files are needed.  If "what" is 0, filespec must be
  19.       the filespec for which matching filenames are needed.  If "what" is 1,
  20.       nextfile() does not use "filespec" and "filespec" should be NULL to
  21.       avoid an assertion error during debugging.
  22.    fileset:  nextfile() can keep track of more than one set of filespecs.
  23.       The fileset specifies which filespec is being matched and therefore
  24.       which set of files is being considered.  "fileset" can be in the
  25.       range 0:FMAX.  Initialization of one fileset does not affect the
  26.       other filesets.
  27.  
  28. OUTPUT
  29.    IF what == 0 THEN
  30.       return value is NULL
  31.    ELSE IF what == 1 THEN
  32.       IF a matching filename is found THEN
  33.          return value is pointer to matching filename including supplied path
  34.       ELSE
  35.          IF at least one file matched previously but no more match THEN
  36.             return value is NULL
  37.          ELSE IF supplied filespec never matched any filename THEN
  38.             IF this is the first call with what == 1 THEN
  39.                return value is pointer to original filespec
  40.             ELSE
  41.                return value is NULL
  42.             END IF
  43.          END IF
  44.       END IF
  45.    END IF
  46.  
  47. NOTE
  48.  
  49.    Initialization done when "what"=0 is not dependent on the correctness
  50.    of the supplied filespec but simply initializes internal variables
  51.    and makes a local copy of the supplied filespec.  If the supplied
  52.    filespec was illegal, the only effect is that the first time that
  53.    nextfile() is called with "what"=1, it will return the original
  54.    filespec instead of a matching filename.  That the filespec was
  55.    illegal will become obvious when the caller attempts to open the
  56.    returned filename for input/output and the open attempt fails.
  57.  
  58. USAGE HINTS
  59.  
  60. nextfile() can be used in the following manner:
  61.  
  62.       char *filespec;                  -- will point to filespec
  63.       char *this_file;                 -- will point to matching filename
  64.       filespec = parse_command_line(); -- may contain wildcards
  65.       FILE *stream;
  66.  
  67.       nextfile (0, filespec, 0);          -- initialize fileset 0
  68.       while ((this_file = nextfile(1, (char *) NULL, 0)) != NULL) {
  69.          stream = fopen (this_file, "whatever");
  70.          if (stream == NULL)
  71.             printf ("could not open %s\n", this_file);
  72.          else
  73.             perform_operations (stream);
  74.       }
  75. */
  76.  
  77. char *nextfile (what, filespec, fileset)
  78. int what;                        /* whether to initialize or match      */
  79. register char *filespec;         /* filespec to match if initializing   */
  80. register int fileset;            /* which set of files                  */
  81. {
  82.     static struct _dta new_dta [FMAX+1];     /* our own private dta        */
  83.     static int first_time [FMAX+1];
  84.                                             /* holds a pathname to return */
  85.     static char pathholder [FMAX+1][FILENAME_MAX];
  86.                                             /* our own copy of filespec   */
  87.     static char saved_fspec [FMAX+1][FILENAME_MAX];
  88.     static done;
  89.  
  90.    assert(fileset >= 0 && fileset <= FMAX);
  91.    if (what == 0) {
  92.       assert(filespec != NULL);
  93.       strcpy (saved_fspec[fileset], filespec);  /* save the filespec */
  94.       first_time[fileset] = 1;
  95.       return ((char *) NULL);
  96.    }
  97.  
  98.    Fsetdta (&new_dta[fileset]);   /* set new dta -- our very own */
  99.    assert(what == 1);
  100.    assert(filespec == NULL);
  101.    assert(first_time[fileset] == 0 || first_time[fileset] == 1);
  102.  
  103.    if (first_time[fileset])            /* first time -- initialize etc. */
  104.        /* find first matching file */
  105.        done = Fsfirst(saved_fspec[fileset], 0);
  106.    else
  107.        /* find next matching file */
  108.        done = Fsnext();
  109.  
  110.    if (done) {                         /* if error status                  */
  111.       if (first_time[fileset]) {       /*   if file never matched then     */
  112.          first_time[fileset] = 0;
  113.          return (saved_fspec[fileset]);/*      return original filespec    */
  114.       } else {                         /*   else                           */
  115.          first_time[fileset] = 0;      /*                                  */
  116.          return ((char *) NULL);         /*      return (NULL) for no more   */
  117.       }
  118.    } else {                                        /* a file matched */
  119.       first_time[fileset] = 0;
  120.       /* add path info  */
  121.       fcbpath (&new_dta[fileset], saved_fspec[fileset], pathholder[fileset]);
  122.       return (pathholder[fileset]);                /* matching path  */
  123.    }
  124. } /* nextfile */
  125.  
  126.  
  127.  
  128. /*
  129. fcbpath() accepts a pointer to the Disk Transfer Area, a character
  130. pointer to a pathname that may contain wildcards, and a character
  131. pointer to a buffer.  It copies into the buffer the path prefix from
  132. the pathname and the filename prefix from the DTA so that it forms a
  133. complete path.
  134. */
  135.  
  136. void fcbpath (dta, old_path, new_path)
  137. struct _dta *dta;
  138. char *old_path;
  139. register char *new_path;
  140. {
  141.    register int i;
  142.    int length, start_pos;
  143.  
  144.    strcpy(new_path, old_path);               /* copy the whole thing first */
  145.    length = strlen(new_path);
  146.    i = length - 1;                           /* i points to end of path */
  147.    while (i >= 0 && new_path[i] != '/' && new_path[i] != '\\' && new_path[i] != ':')
  148.       i--;
  149.    /* either we found a "/", "\", or ":", or we reached the beginning of
  150.       the name.  In any case, i points to the last character of the
  151.       path part. */
  152.    start_pos = i + 1;
  153.    for (i = 0; i < 13; i++)
  154.       new_path[start_pos+i] = dta->dta_name[i];
  155.    new_path[start_pos+13] = '\0';
  156. }
  157.